/// [Special Pythagorean triplet](https://projecteuler.net/problem=9)
fn main() {
    for sum in &[12, 1000] {
        match special_pythagorean_triplet(*sum) {
            Ok(list) => {
                for (a, b, c) in list {
                    println!("The Pythagorean triplet is {} + {} + {} = {}", a, b, c, sum);
                }
            }
            Err(err) => println!("{}", err),
        }
    }
}
/// 遍历，b一定是偶数
fn special_pythagorean_triplet(sum: u32) -> Result<Vec<(u32, u32, u32)>, &'static str> {
    let mut rst: Vec<(u32, u32, u32)> = vec![];
    let max_a = sum / 3;
    let max_b = sum / 2;
    for a in 0..=max_a {
        for b in (a + 1)..=max_b {
            let c = sum - a - b;
            if c <= b {
                break;
            }
            if a.pow(2) + b.pow(2) == c.pow(2) {
                rst.push((a, b, c));
            }
        }
    }
    if rst.len() < 1 {
        Err("no solution")
    } else {
        Ok(rst)
    }
}
